﻿/**
 *	html/monitor/center/scene/panel_tunnel.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-07-15		create the file
 */

/**
 *	create the tunnel panel
 *
 *	@panel_center		[in] the center panel object
 *	@showflag			[in] the show flag ( "show" or "hide" )
 *
 *	return
 *		void
 */
function create_tunnel_panel( panel_center, showflag )
{
    var panel = new Ext.panel.Panel
    ({
         id:"panel_c_tunnel",
         layout:"vbox",
         flex:1,
         width:"100%",
         listeners:{
             afterrender:_create_tunnel_component,
             show:on_panel_show,
         }
    });

    if ( "show" === showflag )
        panel.show();
    else
        panel.hide();

    panel_center.add_subpanel( panel );
}

/**
 *	create component for tunnel panel
 *
 *	@panel_c_tunnel			[in] the tunnel panel object
 */
function _create_tunnel_component( panel_c_tunnel )
{
    var tunnel_top_bar		= _create_tunnel_top_bar();
    var tunnel_grid			= _create_tunnel_grid();

    panel_c_tunnel.add( tunnel_top_bar );
    panel_c_tunnel.add( tunnel_grid );
}

/**
 *	create tunnel top bar
 *
 *	return
 *		the tunnel top bar object
 */
var g_tunnel_top_bar;
function _create_tunnel_top_bar()
{
    g_tunnel_top_bar = Ext.create
    ("Ext.toolbar.Toolbar", {
         id:"tunnel_top_bar", height:30, width:"100%"
    });

    g_tunnel_top_bar.add(
        {xtype:"tbspacer", width:10},
        {id:"ptunnel_tunnel_type_combox", xtype:"combobox",	fieldLabel:"通道类型", labelWidth:50, width:200,
            queryMode:"local", store:g_tunnel_type, valueField:"serial", value:0, displayField:"type"},
        {xtype:"tbspacer", width:10},
        {xtype:"button", text:"查询", frame:true, handler:on_search_tunnel_type, width:60},
        "->",
        {xtype:"button", text:"添加", handler:on_add_tunnel},
        "-",
        {xtype:"button", text:"修改", handler:on_modify_tunnel},
        "-",
        {xtype:"button", text:"删除", handler:on_delete_tunnel}
    );

    return g_tunnel_top_bar;
}

/**
 *	create tunnel grid
 *
 *	return
 *		the tunnel grid object
 */
var g_tunnel_grid;
function _create_tunnel_grid()
{
    g_tunnel_grid	= Ext.create
    ("Ext.grid.Panel",{
         id:"ptunnel_tunnel_grid", width:"100%", flex:1, store:g_tunnel,
         selType:"rowmodel", multiSelect:true,
         columns:[
             {header:"序号", xtype:"rownumberer", width:50, align:"center"},
             {header:"主机编号", width:150, dataIndex:"host", align:"center"},
             {header:"通道名称", width:150, dataIndex:"name", align:"center"},
             {header:"通道类型", width:100, dataIndex:"type", align:"center"},
             {header:"通道编号", width:100, dataIndex:"number", align:"center"},
             {header:"备注说明", width:200, dataIndex:"remark"},
         ]
    });

    return g_tunnel_grid;
}

/**
 *	show the tunnel
 */
function on_panel_show()
{
    on_search_tunnel_type();
}

/**
 *	search tunnel type button handler
 */
function on_search_tunnel_type()
{
    var cmp		= Ext.getCmp( "ptunnel_tunnel_type_combox" );
    var word 	= cmp.getRawValue();

    g_tunnel.filterBy( function( record ){
        if ( "全部" === word )
            return true;
        else
            return record.get( "type" ) === word;
    });
}

/**
 *	add tunnel
 */
var g_form_tunnel_edit;
function on_add_tunnel()
{
    if ( undefined === g_form_tunnel_edit )
        g_form_tunnel_edit = _create_form_tunnel_edit();
    g_form_tunnel_edit.show_model	= "add";
    g_form_tunnel_edit.setTitle( "添加通道" );

    _enable_windows( false );
    g_form_tunnel_edit.show();
}

/**
 *	modify tunnel
 */
function on_modify_tunnel()
{
    var record = g_tunnel_grid.getSelectionModel().getLastSelected();

    if ( undefined === g_form_tunnel_edit )
        g_form_tunnel_edit = _create_form_tunnel_edit();
    g_form_tunnel_edit.show_model	= "modify";
    g_form_tunnel_edit.setTitle( "修改通道" );

    if ( null == record )
    {
        alert( "请选择一个要修改的通道!" );
        return;
    }

    g_form_tunnel_edit.loadRecord( record );

    _enable_windows( false );
    g_form_tunnel_edit.show();
}

/**
 *	delete tunnels
 */
function on_delete_tunnel()
{
    var records = g_tunnel_grid.getSelectionModel().getSelection();
    var res		= 0;

    for( var i = 0; i < records.length; i++ )
    {
        var		rcd = records[i];
        res = _check_tunnel_used( rcd.get("host"), rcd.get("number") );
        if ( 0 !== res ) { alert( g_error[res] ); return; }
        delete_tunnel_record( rcd );
        g_tunnel.remove( rcd );
    }
}

/**
 *	create tunnel edit form
 */
function _create_form_tunnel_edit()
{
    var	form_tunnel_edit = Ext.create
    ("Ext.form.Panel", {
         title:"编辑通道配置", frame:true, renderTo:Ext.getBody(),
         draggable:true, constrain:true, simpleDrag:true, floating:true,
         width:311, height:249, resizable:false, buttonAlign:"center",
         listeners:{
             beforeclose:function()
             {
                 form_tunnel_edit_cancel_button();
                 return false;
             }
         },
         items:[
             {name:"host", xtype:"combobox",	fieldLabel:"主机编号", labelWidth:60, width:300,
                 queryMode:"local", maxHeight:60, store:g_host, valueField:"host", displayField:"host"},
             {name:"name", xtype:"textfield",	fieldLabel:"通道名称", labelWidth:60, width:300},
             {name:"type", xtype:"combobox",	fieldLabel:"通道类型", labelWidth:60, width:300,
                 queryMode:"local", maxHeight:60, store:g_tunnel_type, valueField:"type", displayField:"type"},
             {name:"number", xtype:"textfield",	fieldLabel:"通道编号", labelWidth:60, width:300},
             {name:"remark", xtype:"textarea",	fieldLabel:"备注说明", labelWidth:60, width:300},
         ],
         buttons:[
             {text:"确定", handler:form_tunnel_edit_ok_button},
             {text:"取消", handler:form_tunnel_edit_cancel_button}
         ]
    });

    form_tunnel_edit.hide();

    return form_tunnel_edit;
}

/**
 *	ok button for edit tunnel
 */
function form_tunnel_edit_ok_button()
{
    var values 	= g_form_tunnel_edit.getValues();
    var res 	= 0;

    if ( "add" === g_form_tunnel_edit.show_model )
    {
        res = _check_tunnel_add( values );
        if ( 0 !== res ) { alert( g_error[res] ); return; }

        var count = g_tunnel.count();
        g_tunnel.insert( count, [[count, values["name"], values["type"], values["number"], values["host"], values["remark"], 1]] );
    }
    else if ( "modify" === g_form_tunnel_edit.show_model )
    {
        var record	= g_form_tunnel_edit.getRecord();
        res = _check_tunnel_modify( values, record );
        if ( 0 !== res ) { alert( g_error[res] ); return; }

        g_form_tunnel_edit.updateRecord();
        record.commit();
    }

    update_tunnel_record( values );

    g_form_tunnel_edit.hide();
    _enable_windows( true );
}

/**
 *	check tunnel values for adding
 */
function _check_tunnel_add( values )
{
    var		res 	= 0;
    var		number	= values["number"];
    var		type	= values["type"];
    var		host	= values["host"];
    var		name	= values["name"];

    number	= Ext.String.trim( number );
    type	= Ext.String.trim( type );
    host	= Ext.String.trim( host );
    name	= Ext.String.trim( name );

    if ( "" === host )		return 0x203;
    if ( "" === name )		return 0x303;
    if ( "" === number )	return 0x204;
    if ( "" === type )		return 0x302;

    res = _check_tunnel_number( host, number );

    return res;
}

/**
 *	check tunnel values if it's already exists
 *
 *	@new_host			[in] the new host field
 *	@new_num			[in] the new number field
 *
 *	return
 *		==	0	ok
 *		!=	0	errorcode
 */
function _check_tunnel_number( new_host, new_num )
{
    var		count	= g_tunnel.count();
    for ( var i = 0; i < count; i++ )
    {
        var rcd = g_tunnel.getAt( i );
        if ( rcd.get("host") === new_host && rcd.get("number") === new_num )
           return 0x301;
    }

    return 0;
}

/**
 *	check tunnel values if it can be modified
 *
 *	@values			[in] the new value set
 *	@record			[in] the old record
 *
 *	return
 *		==	0	ok
 *		!=	0	errorcode
 */
function _check_tunnel_modify( values, record )
{
    var new_num		= values["number"];
    var new_host	= values["host"];
    var old_num		= record.get( "number" );
    var old_host	= record.get( "host" );

    if ( new_num === old_num && new_host === old_host )
        return 0;

    return _check_tunnel_used( old_host, old_num );
}

/**
 *	check tunnel values if it can be modified
 *
 *	@old_host		[in] the old host field
 *	@old_num		[in] the old number field
 *
 *	return
 *		==	0	ok
 *		!=	0	errorcode
 */
function _check_tunnel_used( old_host, old_num )
{
    var count = g_device.count();
    var rcd_host;
    var	rcd_num;
    var rcd;

    for ( var i = 0; i < count; i++ )
    {
        rcd 		= g_device.getAt( i );
        rcd_num		= rcd.get("tunnel_num");
        rcd_host	= rcd.get("host");
        if ( rcd_host === old_host && rcd_num === old_num )		// check old tunnel info, if any device uses it
            return 0x310;
    }

    return 0;
}

/**
 *	cancel button for edit tunnel
 */
function form_tunnel_edit_cancel_button()
{
    g_form_tunnel_edit.hide();
    _enable_windows( true );
}
